package com.funtonow.jqdk.fkcs.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.funtonow.jqdk.R;
import com.funtonow.jqdk.adapter.FKDateAdapter;
import com.funtonow.jqdk.base.BaseFragment;
import com.funtonow.jqdk.base.Constants;
import com.funtonow.jqdk.base.SqjAppliaction;
import com.funtonow.jqdk.base.UrlUtils;
import com.funtonow.jqdk.bean.LoanInfoBean;
import com.funtonow.jqdk.bean.UserInfoBean;
import com.funtonow.jqdk.fkcs.ui.BankActivity;
import com.funtonow.jqdk.fkcs.ui.CardActivity;
import com.funtonow.jqdk.fkcs.ui.DkFailActivity;
import com.funtonow.jqdk.ui.ExamineFailActivity;
import com.funtonow.jqdk.ui.LoginActivity;
import com.funtonow.jqdk.ui.NewsActivity;
import com.funtonow.jqdk.ui.UserInfoActivity;
import com.funtonow.jqdk.utils.HttpUtil;
import com.funtonow.jqdk.utils.SPUtil;
import com.funtonow.jqdk.utils.StringUtil;
import com.funtonow.jqdk.utils.TimeUtils;
import com.funtonow.jqdk.widget.ProgressDialogUtil;
import com.superluo.textbannerlibrary.TextBannerView;
import com.umeng.analytics.MobclickAgent;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

import static com.funtonow.jqdk.base.UrlUtils.DK_ONLINE_NUM;
import static com.funtonow.jqdk.base.UrlUtils.HOME_LIST;

/**
 * @author Jiajien
 * @date 2019/7/12
 */
public class FkHomeFragment extends BaseFragment implements View.OnClickListener {

    @BindView(R.id.tv_banner)
    TextBannerView tvBanner;
    @BindView(R.id.rlv_date)
    RecyclerView rlvDate;
    @BindView(R.id.sb_money)
    SeekBar sbMoney;
    @BindView(R.id.tv_num_money)
    TextView tvNumMoney;

    private LoanInfoBean loanInfoBean;

    private List<String> strings = new ArrayList<>();
    private FKDateAdapter fkDateAdapter;

    public static FkHomeFragment newInstance() {
        Bundle args = new Bundle();
        FkHomeFragment fragment = new FkHomeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View getLayout(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.framgent_fk_home, null);
    }

    @Override
    public void onCreateFragment(@Nullable Bundle savedInstanceState) {
        initToolbar(getResources().getString(R.string.ssq_loan_title));
        mIbReturn.setVisibility(View.GONE);
        ibNews.setVisibility(View.VISIBLE);
        initReclyView(rlvDate);
        getAppId();
        getDkNum();
        if (TimeUtils.isShowNewsHot()) {
            setRightDrawable(R.mipmap.icon_news_hot);
        } else {
            setRightDrawable(R.mipmap.icon_news);
        }
        fkDateAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                fkDateAdapter.setPosition(position);
                fkDateAdapter.notifyDataSetChanged();
            }
        });
        sbMoney.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tvNumMoney.setText(String.format(Locale.getDefault(), "%d元", (progress * 100) + 1000));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    /**
     * 设置 RecyclerView
     *
     * @param recyclerView
     */
    private void initReclyView(RecyclerView recyclerView) {
        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 3);
        recyclerView.setLayoutManager(layoutManager);
        setData();
        fkDateAdapter = new FKDateAdapter(strings);
        recyclerView.setAdapter(fkDateAdapter);
    }


    private void setData() {
        strings.add("7天");
        strings.add("14天");
        strings.add("21天");
        strings.add("28天");
        strings.add("30天");
    }

    /**
     * 获取数据appId
     */
    private void getAppId() {
        ProgressDialogUtil.startProgressDialog(getActivity());
        HttpUtil.getInstance().getDataAsynFromNet(UrlUtils.GET_APPID, new HttpUtil.MyNetCall() {
            @Override
            public void success(Call call, String jsonString) throws IOException {
                Constants.DATA_APP_ID = JSON.parseObject(jsonString).getString("appId");
                getLoanList();
            }

            @Override
            public void failed(Call call, IOException e) {

            }
        });
    }

    private void getLoanList() {
        Map<String, String> param = new HashMap<>();
        param.put("appid", Constants.DATA_APP_ID);
        param.put("app", "1");
        HttpUtil.getInstance().postDataAsynToNet(HOME_LIST, param, new HttpUtil.MyNetCall() {
            @Override
            public void success(Call call, String jsonString) throws IOException {
                ProgressDialogUtil.stopProgressDialog();
                loanInfoBean = JSON.parseObject(jsonString, LoanInfoBean.class);
                SqjAppliaction.loanInfoBean = loanInfoBean;
                SPUtil.put(Constants.CONFIG_INFO, SPUtil.beanToJson(loanInfoBean.getInfo()));

            }

            @Override
            public void failed(Call call, IOException e) {

            }
        });
    }

    @OnClick({R.id.ib_news, R.id.tv_loan})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ib_news:
                setRightDrawable(R.mipmap.icon_news);
                MobclickAgent.onEvent(SqjAppliaction.getContext(), "gonglue");
                startActivity(new Intent(getActivity(), NewsActivity.class));
                break;
            default:
                if (SPUtil.getUserInfo() == null) {
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    UserInfoBean userInfoBean = SPUtil.getUserInfo();
                    if (StringUtil.isEmpty(userInfoBean.getNickname())) {
                        startActivity(new Intent(getActivity(), CardActivity.class));
                    } else if (StringUtil.isEmpty(userInfoBean.getDetail().getBankNum())) {
                        startActivity(new Intent(getActivity(), BankActivity.class));
                    } else {
                        startActivity(new Intent(getActivity(), UserInfoActivity.class));
                    }
                }
                break;
        }
    }

    /**
     * 累计放款
     */
    private void getDkNum() {
        Map<String, String> param = new HashMap<>();
        HttpUtil.getInstance().postDataAsynToNet(DK_ONLINE_NUM, param, new HttpUtil.MyNetCall() {
            @Override
            public void success(Call call, String jsonString) {
                JSONObject jsonObject = JSON.parseObject(jsonString);
                tvBanner.setDatas(Arrays.asList(jsonObject.getJSONObject("data").getJSONObject("msg").getString("T")
                        .split("\n")));
            }

            @Override
            public void failed(Call call, IOException e) {

            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        tvBanner.startViewAnimator();
    }

    @Override
    public void onPause() {
        super.onPause();
        tvBanner.stopViewAnimator();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ProgressDialogUtil.stopProgressDialog();
    }
}
